<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Modsettings extends Model {
    
    function Modsettings() {
    	
        parent::Model();
        
        $this->load->library('table');
    }    
    
    public function settings($modul) {
        
        $this->table->set_heading('Опция', 'Значение');
        
        $query = $this->db->get_where('settings', array('modul' => $modul));
                
        foreach ($query->result_array() as $row) {
            
            $this->table->add_row($row['title'], $this->input_by_id($row['tip'], $row['name'], $row['value']));
        }
        
        return $this->table->generate();
    }
    
    public function update($modul = NULL) {
        
        $query = $this->db->get_where('settings', array('modul' => $modul));
        
        foreach ($query->result_array() as $row) {
            
            $data['value'] = $this->input->post($row['name']);
            
            $this->db->update('settings', $data, array('id' => $row['id']));
        }
    }
    
    private function input_by_id($id, $name, $value) {
        
        switch($id) {
            
            case 1: 
                
                $end[1] = ($value == 1) ? TRUE : FALSE;
                $end[2] = ($value == 0) ? TRUE : FALSE;
                
                return form_radio($name, 1, $end[1]).
                	   form_label(' Да ', $name).
                       form_radio($name, 0, $end[2]).
                       form_label(' Нет ', $name);
                break;
                
            case 2: 
                return form_textarea($name, $value);
                break;
                
            case 3: 
                
                return form_input($name, $value);
                break;
        }
    }
}
?>